    function $(id){
		return document.getElementById(id);
	};
	function searchIndex(value,opid){
		var op=$(opid);
		for (var i=0;i<op.options.length ;i++ ){
			if (op.options[i].value==String(value)){
				op.options[i].selected=true;
				return;
			}
		}
		op.options[0].selected=true;
		return;
	};
	function searchid(id){
		for (var i=0;i<trinkets.length ;i++ ){
			if (trinkets[i]['id']==id){
				return i;
			}
		}
		return Math.rand(0,trinkets.length+1,'int');
	};
	function importlists(){
		function aa(arr,to){
			var Option = $(to);
			for (var i=0;i<arr.length ;i++ ){
				var op = document.createElement("option");
				op.value = arr[i];
				op.innerHTML = arr[i];
				Option.appendChild(op);
			}
		}
		var lists = [];
		for (var i=0;i<trinkets.length ;i++ ){
			lists.push(trinkets[i].id);
		}
		aa(lists,'lists')
		var Option = $('buff_library');
		for (var x in buffs){
			var op = document.createElement("option");
			op.value = x;
			op.innerHTML = x;
			Option.appendChild(op);
		}
		var hero_classes=['crusader', 'highwayman', 'plague_doctor', 'vestal', 'hellion', 'bounty_hunter', 'grave_robber', 'occultist', 'jester', 'leper'];
		aa(hero_classes,'hero_classes');
		var rarities = ['very_common','common','uncommon','rare','very_rare'];
		aa(rarities,'rarities');
	};
	function loadtrinket(id){
		return trinkets[searchid(id)];
	};
	function viewtrinket(trinket){
		$("id").value=trinket.id;
		$("rarity").value=trinket.rarity;
		$("price").value=trinket.price;
		function newlist(id){
			var ops = $(id);
			ops.options.length=0;
			for (var i=0;i<trinket[id].length ;i++ ){
				var op = document.createElement("option");
				op.value = trinket[id][i];
				op.innerHTML = trinket[id][i];
				ops.appendChild(op);
			}
		};
		newlist('buffs');
		newlist('hero_class_requirements');
	};
	function addoptions(from,to){
		var ofrom = $(from),oto = $(to),index=ofrom.selectedIndex;
		index=index<0?0:index;
		if (ofrom.options.length<1){
			return;
		}
		var toadd = ofrom.options[index].value;
		var op = document.createElement("option");
		op.value = toadd;
		op.innerHTML = toadd;
		oto.appendChild(op);
	};
	function removeoptions(from){
		var ofrom = $(from),index = ofrom.selectedIndex;
		index=index<0?0:index;
		ofrom.options.remove(index);
		if (ofrom.options.length<1){
			return;
		}
		if (index>=ofrom.options.length){
			ofrom.options[ofrom.options.length-1].selected=true;
		}
		else{
			ofrom.options[index].selected=true;
		}
	};
	function attach(){
		importlists();
		$("lists").onchange=function(){$("id").value=this.value;};
		$("import").onclick=function(){
			viewtrinket(loadtrinket($("id").value));
		};
		$("outport").onclick=function(){$('content').value=js_beautify(JSON.stringify(outport()));};
		$("save").onclick=function(){
			var id = $("id").value;
			for (var i=0;i<trinkets.length ;i++ ){
				if (trinkets[i].id==id){
					trinkets[i]=outport();
					return;
				}
			}
			trinkets.push(outport());
			importlists();
			return;
		};
		$("localization").onclick=function(){
			var arr=[];
			for (var i=0;i<trinkets.length ;i++ ){
				arr.push(JSON.stringify(trinkets[i]));
			}
			$('content').value=js_beautify('{"trinkets":['+arr+']}', 1, '\t');
		};
		$("loot").onclick=function(){
			var arr=[];
			var rarity = {'very_common':3,'common':3,'uncommon':2,'rare':2,'very_rare':1};
			for (var i=0;i<trinkets.length ;i++ ){
				var json = {
					"type": "item",
					"chances": rarity[trinkets[i].rarity],
					"data": {
						"type": "trinket",
						"id": trinkets[i].id,
						"amount": 1
					}
				};
				arr.push(json);
			}
			var tables = loots["loot_tables"];
			for (var i=0;i<tables.length ; i++){
				if (tables[i].id=="T"){
					tables[i].entries.length=1;
					tables[i].entries=tables[i].entries.concat(arr);
				}
			}
			loots["loot_tables"] = tables;
			$('content').value=js_beautify(JSON.stringify(loots), 1, '\t');
		};
		$("random").onclick=function(){
			var str = new trinket(0,loadtrinket($("id").value)).string;
			viewtrinket(JSON.parse(str));
		};
		$("randomall").onclick=function(){
			var arr = [];
			for (var i=0;i<trinkets.length ;i++ ){
				arr.push(trinket(0,trinkets[i]).string);
			}
			$('content').value=js_beautify('{"trinkets":['+arr+']}', 1, '\t');
		};
		$('new').onclick=function(){
			var number = Number(prompt('Please input the number of trinkets',''));
			var arr=[],str='',json={};
			for (var i=0;i<number ;i++ ){
				var o = new trinket();
				if (!json[o.id]){
					json[o.id]=o.id;
					arr.push(JSON.parse(o.string));
					str += (o.translation+'\n');
				}
				
			}
			trinkets = trinkets.concat(arr);
			importlists();
			$('content').value = str;
			
		};
		$("subbuffs").onclick=function(){removeoptions('buffs');};
		$("addbuffs").onclick=function(){addoptions('buff_library','buffs');};
		$("subclasses").onclick=function(){
			removeoptions('hero_class_requirements');
			addoptions('hero_class_requirements','hero_classes');
		};
		$("addclasses").onclick=function(){
			addoptions('hero_classes','hero_class_requirements');
			removeoptions('hero_classes');
		};
		$('rarities').onchange=function(){var db={'very_common':5000,'common':7500,'uncommon':10000,'rare':15000,'very_rare':25000};$('rarity').value=this.value;$('price').value=db[this.value];};
	};
	function outport(){
		var getlist=function(id){
			var ops=$(id);
			var arr=[];
			for (var i=0;i<ops.options.length ;i++ ){
				arr.push(ops.options[i].value);
			}
			return arr;
		}
		var json={
			"id" : $("id").value,
			"buffs" : getlist("buffs"),
			"hero_class_requirements":getlist("hero_class_requirements"),
			"rarity" : $("rarity").value,
			"price" : Number($("price").value)
		};
		return json;
	};